package com.lianwei.hospital.controller;

import com.lianwei.hospital.Service.DoctorService;
import com.lianwei.hospital.module.entity.Admain;
import com.lianwei.hospital.module.entity.DoctorInfo;
import com.lianwei.hospital.module.vo.DoctorVO;
import com.lianwei.hospital.utils.JwtUtils;
import com.lianwei.hospital.utils.PageUtil;
import com.lianwei.hospital.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/doctor")
public class DoctorInfoController {
    @Autowired
    private DoctorService doctorService;

    @PostMapping("/login")
    public Result<DoctorInfo> login(@RequestParam("username") String id, @RequestParam("password") String password, HttpSession session) throws Exception{
        DoctorInfo doctorInfo= doctorService.login(id, password);
        if (doctorInfo!=null){
            session.setAttribute("doctorInfo",doctorInfo);
//            String res= JwtUtils.sign(id, String.valueOf(doctorInfo.getId()));
//            System.out.println(res);
            return Result.buildResult(Result.Status.OK,doctorInfo);
        }
        return Result.buildResult(Result.Status.BAD_REQUEST);
    }

    @GetMapping("/selectById")
    public Result<DoctorInfo> Select(String i) throws Exception {
        System.out.println(i+"\n\n");
        DoctorInfo doctorInfo = doctorService.selectByPrimaryKey(i);
        return Result.buildResult(Result.Status.OK,doctorInfo);
    }

    @PutMapping
    public Result<List<DoctorInfo>> Insert(@RequestBody DoctorInfo doctorInfo) throws Exception {
        System.out.println(doctorInfo);
        if(doctorService.insert(doctorInfo)){
            List<DoctorInfo> doctorInfoList = doctorService.selectAll();
            return Result.buildResult(Result.Status.OK,doctorInfoList);
        }
        return Result.buildResult(Result.Status.BAD_REQUEST);
    }

    @DeleteMapping
    public Result<List<DoctorInfo>> Delete(@RequestBody DoctorInfo doctorInfo) throws Exception {
        System.out.println(doctorInfo);
        if(doctorService.deleteByPrimaryKey(doctorInfo.getId())){
            List<DoctorInfo> doctorInfoList = doctorService.selectAll();
            return Result.buildResult(Result.Status.OK,doctorInfoList);
        }
        return Result.buildResult(Result.Status.BAD_REQUEST);
    }

    @PostMapping
    public Result<List<DoctorInfo>> Update(@RequestBody DoctorInfo doctorInfo) throws Exception {
        System.out.println(doctorInfo);
        if(doctorService.updateByPrimaryKey(doctorInfo)){
            List<DoctorInfo> doctorInfoList = doctorService.selectAll();
            return Result.buildResult(Result.Status.OK,doctorInfoList);
        }
        return Result.buildResult(Result.Status.BAD_REQUEST);
    }

    @GetMapping("/selectAll")
    public Result<PageUtil<DoctorInfo>> list(@RequestBody DoctorVO query) throws Exception {
        PageUtil<DoctorInfo> pageUtil=doctorService.findPage(query);
        return Result.buildResult(Result.Status.OK,pageUtil);
    }
}
